package com.hogan.hoganproject;

import android.Manifest;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.ashokvarma.bottomnavigation.BadgeItem;
import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.hogan.hoganproject.base.activity.BaseActivity;
import com.hogan.hoganproject.base.fragment.BaseFragment;
import com.hogan.hoganproject.contacts.ContactsFragment;
import com.hogan.hoganproject.home.HomeFragment;
import com.hogan.hoganproject.video.VideoFragment;
import com.hogan.hoganproject.settings.SettingsFragment;
import com.permissionx.guolindev.PermissionX;
import com.permissionx.guolindev.callback.ExplainReasonCallbackWithBeforeParam;
import com.permissionx.guolindev.callback.ForwardToSettingsCallback;
import com.permissionx.guolindev.callback.RequestCallback;
import com.permissionx.guolindev.request.ExplainScope;
import com.permissionx.guolindev.request.ForwardScope;

import java.util.List;

//todo https://blog.csdn.net/c__chao/article/details/78573737 免费接口

public class MainActivity extends BaseActivity implements BottomNavigationBar.OnTabSelectedListener {
    private BottomNavigationBar mBottomNavigationBar;
    private final static String TAG = "Fragment_Item_";
    private BaseFragment mCurFragment;
    private HomeFragment mHomeFragment;
    private ContactsFragment mContactsFragment;
    private VideoFragment mVideoFragment;
    private SettingsFragment mSettingsFragment;

    @Override
    public int getLayoutResID() {
        return R.layout.activity_main;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        PermissionX.init(this)

                .permissions(Manifest.permission.READ_CONTACTS, Manifest.permission.CALL_PHONE)

                .onExplainRequestReason(new ExplainReasonCallbackWithBeforeParam() {

                    @Override

                    public void onExplainReason(ExplainScope scope, List<String> deniedList, boolean beforeRequest) {

                        scope.showRequestReasonDialog(deniedList, "即将申请的权限是程序必须依赖的权限", "我已明白");

                    }

                })

                .onForwardToSettings(new ForwardToSettingsCallback() {

                    @Override

                    public void onForwardToSettings(ForwardScope scope, List<String> deniedList) {

                        scope.showForwardToSettingsDialog(deniedList, "您需要去应用程序设置当中手动开启权限", "我已明白");

                    }

                }).request(new RequestCallback() {

            @Override

            public void onResult(boolean allGranted, List<String> grantedList, List<String> deniedList) {

                if (allGranted) {

                    //   Toast.makeText(this, "所有申请的权限都已通过", Toast.LENGTH_SHORT).show();

                } else {

                    // Toast.makeText(getActivity(), "您拒绝了如下权限：" + deniedList, Toast.LENGTH_SHORT).show();

                }

            }

        });

    }

    @Override
    protected void initActivity() {
        mCurFragment = mHomeFragment = HomeFragment.newInstance();
        addFragment(mHomeFragment, R.id.main_fragment, "home");
        initBottomNavigationBar();
    }

    private void initBottomNavigationBar() {
        mBottomNavigationBar = findViewById(R.id.bootom_navigation_bar);
        mBottomNavigationBar.setTabSelectedListener(this);
        BadgeItem badgeItem = new BadgeItem().setBackgroundColor(Color.RED).setText("1");
        mBottomNavigationBar.setMode(mBottomNavigationBar.MODE_FIXED);
        mBottomNavigationBar.setBackgroundStyle(mBottomNavigationBar.BACKGROUND_STYLE_STATIC);
        mBottomNavigationBar.setBarBackgroundColor(R.color.colorPrimary);
        mBottomNavigationBar.addItem(new BottomNavigationItem(R.mipmap.app_home, getString(R.string.app_home)).setActiveColorResource(R.color.white))
                .addItem(new BottomNavigationItem(R.mipmap.app_contacts, getString(R.string.app_contacts)).setActiveColorResource(R.color.white))
                .addItem(new BottomNavigationItem(R.mipmap.app_video, R.string.app_video).setActiveColorResource(R.color.white).setBadgeItem(badgeItem))
                .addItem(new BottomNavigationItem(R.mipmap.app_settings, getString(R.string.app_settings)).setActiveColorResource(R.color.white))
                .setFirstSelectedPosition(0)
                .initialise(); //所有的设置需在调用该方法前完成
    }

    @Override
    public void onTabSelected(int position) {
        switch (position) {
            case 0:
                if (mHomeFragment == null) {
                    mHomeFragment = HomeFragment.newInstance();
                }
                switchContent(mCurFragment, mHomeFragment, TAG + position);
                break;
            case 1:
                if (mContactsFragment == null) {
                    mContactsFragment = ContactsFragment.newInstance();
                }
                switchContent(mCurFragment, mContactsFragment, TAG + position);
                break;
            case 2:
                if (mVideoFragment == null) {
                    mVideoFragment = VideoFragment.newInstance();
                }
                switchContent(mCurFragment, mVideoFragment, TAG + position);
                break;
            case 3:
                if (mSettingsFragment == null) {
                    mSettingsFragment = SettingsFragment.newInstance();
                }
                switchContent(mCurFragment, mSettingsFragment, TAG + position);
                break;
        }
    }

    @Override
    public void onTabUnselected(int position) {

    }

    @Override
    public void onTabReselected(int position) {

    }

    private void switchContent(BaseFragment from, BaseFragment to, String tag) {
        if (mCurFragment != to) {
            mCurFragment = to;
//            FragmentTransaction transaction = mFragmentManager.beginTransaction().setCustomAnimations(
//                    android.R.anim.fade_in, android.R.anim.fade_out);
            FragmentTransaction transaction = mFragmentManager.beginTransaction();
            if (!to.isAdded()) {    // 先判断是否被add过
                transaction.hide(from).add(R.id.main_fragment, to, tag).commitAllowingStateLoss(); // 隐藏当前的fragment，add下一个到Activity中
            } else {
                transaction.hide(from).show(to).commitAllowingStateLoss(); // 隐藏当前的fragment，显示下一个
            }
        }
    }
}